/*
 * Copyright (c) 2010-2011 Josef Hardi <josef.hardi@gmail.com>
 * Copyright (c) 2010 Piotr Zagawa <piotr.zagawa@gmail.com>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package id.duapuluhdua.dakidaki.layers;

import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;

import id.duapuluhdua.dakidaki.Command;
import id.duapuluhdua.dakidaki.MainDraw;
import id.duapuluhdua.dakidaki.R;
import id.duapuluhdua.dakidaki.common.CommonActivity;
import id.duapuluhdua.dakidaki.compass.CompassRotation;
import id.duapuluhdua.dakidaki.map.Screen;

/**
 *
 */
public class LayerCompass extends Layer
{
  /** Fields */
  private static final int RADIUS = 70;
  private static final int HOVER = 4;

  private Paint mShape;
  private Paint mLine;

  protected Drawable mDirN;
  protected Drawable mDirS;
  protected Drawable mDirW;
  protected Drawable mDirE;

  protected int mDirIconW;
  protected int mDirIconH;

  /**
   * The constructor.
   *
   * @param parent the common user interaction holder.
   * @param screen the screen object.
   */
  public LayerCompass(CommonActivity parent, Screen screen)
  {
    super(parent, screen);

    mDirN = parent.getResources().getDrawable(R.drawable.dir_arrow_n);
    mDirS = parent.getResources().getDrawable(R.drawable.dir_arrow_s);
    mDirW = parent.getResources().getDrawable(R.drawable.dir_arrow_w);
    mDirE = parent.getResources().getDrawable(R.drawable.dir_arrow_e);

    mDirIconW = mDirN.getIntrinsicWidth();
    mDirIconH = mDirS.getIntrinsicHeight();

    // Paint properties for drawing a shape.
    mShape = new Paint();
    mShape.setAntiAlias(true);
    mShape.setColor(0x33408090);
    mShape.setStrokeWidth(1);
    mShape.setStyle(Paint.Style.FILL);

    // Paint properties for drawing a line.
    mLine = new Paint();
    mLine.setAntiAlias(true);
    mLine.setColor(0xff408090);
    mLine.setStrokeWidth(1);
    mLine.setStyle(Paint.Style.STROKE);
  }

  /**
   * Draw the compass with main needles pointing to four main directions
   * (i.e., N, E, S, W) with a blue circle as the center, locating the
   * current position of the user.
   *
   * @param canvas the canvas object.
   */
  @Override
  public void doDraw(Canvas canvas, Rect area)
  {
    if (mState.getCurrentUiMode() == Command.CMD_MODE_MAP) {
      final int centerX = CompassRotation.rotationCenterX();
      final int centerY = CompassRotation.rotationCenterY();

      canvas.drawCircle(centerX, centerY, RADIUS, mShape);
      canvas.drawCircle(centerX, centerY, RADIUS, mLine);

      MainDraw.compassRotation.set(canvas);

      // Draw compass needles
      canvas.drawLine(centerX - RADIUS - HOVER,
          centerY,
          centerX + RADIUS + HOVER,
          centerY, mLine);
      canvas.drawLine(centerX,
          centerY - RADIUS - HOVER,
          centerX,
          centerY + RADIUS + HOVER,
          mLine);

      // Draw compass direction pointers
      final int top_WE = centerY - (mDirIconH >> 1);  // half
      final int left_NS = centerX - (mDirIconW >> 1);
      final int top_N = centerY - RADIUS - HOVER - mDirIconH;
      final int top_S = centerY + RADIUS + HOVER;
      final int left_W = centerX - RADIUS - HOVER - mDirIconH;
      final int left_E = centerX + RADIUS + HOVER;

      mDirN.setBounds(left_NS, top_N, left_NS + mDirIconW, top_N + mDirIconH);
      mDirN.draw(canvas);

      mDirS.setBounds(left_NS, top_S, left_NS + mDirIconW, top_S + mDirIconH);
      mDirS.draw(canvas);

      mDirW.setBounds(left_W, top_WE, left_W + mDirIconW, top_WE + mDirIconH);
      mDirW.draw(canvas);

      mDirE.setBounds(left_E, top_WE, left_E + mDirIconW, top_WE + mDirIconH);
      mDirE.draw(canvas);

      MainDraw.compassRotation.clear(canvas);
    }
  }

  @Override
  public void updateObjectsState()
  {
    // Do nothing.
  }

  @Override
  public void surfaceSizeChanged(int width, int height, int screenOrientation)
  {
    // Do nothing.
  }

  @Override
  public void initData()
  {
    // Do nothing.
  }
}
